package factory.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import dataAsset.domain.DataAssetFieldInfo;
import dataAsset.domain.DataAssetInfo;
import dataAsset.dto.DataAssetDTO;
import dataAsset.vo.DataAssetQueryOneVO;
import dataAsset.vo.DataAssetVO;
import factory.mapper.DataAssetMapper;
import factory.service.DataAssetFieldService;
import factory.service.DataAssetService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import result.PageDTO;
import utils.BeanConverter;

import java.util.List;

@Service
public class DataAssetServiceImpl extends ServiceImpl<DataAssetMapper, DataAssetInfo>
                                      implements DataAssetService {

    @Resource
    private DataAssetFieldService dataAssetFieldService;
    @Override
    public Page<DataAssetVO> pageAll(PageDTO pageDTO) {
        Page<DataAssetInfo> page = new Page<>(pageDTO.getPageNumber(), pageDTO.getPageSize());
        Page<DataAssetInfo> pageResult = this.page(page);
        Page<DataAssetVO> returnPage = new Page<>();
        returnPage.setRecords(BeanConverter.convertList(pageResult.getRecords(),DataAssetVO.class));
        returnPage.setCurrent(pageResult.getCurrent());
        returnPage.setTotal(pageResult.getTotal());
        returnPage.setSize(pageResult.getSize());
        return returnPage;
    }

    @Override
    public Page<DataAssetVO> pageAllByQuery(DataAssetDTO dataAssetDTO) {
        Page<DataAssetInfo> page = new Page<>(dataAssetDTO.getPageNumber(), dataAssetDTO.getPageSize());
        LambdaQueryWrapper<DataAssetInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DataAssetInfo::getStaticStatus,dataAssetDTO.getStaticStatus())
                .like(DataAssetInfo::getDatabaseCname,dataAssetDTO.getDatabaseCname())
                .like(DataAssetInfo::getDatabaseEname,dataAssetDTO.getDatabaseEname());
        Page<DataAssetInfo> pageResult = this.page(page,wrapper);
        Page<DataAssetVO> returnPage = new Page<>();
        returnPage.setRecords(BeanConverter.convertList(pageResult.getRecords(),DataAssetVO.class));
        returnPage.setCurrent(pageResult.getCurrent());
        returnPage.setTotal(pageResult.getTotal());
        returnPage.setSize(pageResult.getSize());
        return returnPage;
    }

    @Override
    public DataAssetQueryOneVO onclickName(Integer id){
        DataAssetInfo one = this.getById(id);
        DataAssetQueryOneVO dataAssetQueryOneVO = new DataAssetQueryOneVO();
        dataAssetQueryOneVO.setDatabaseCname(one.getDatabaseCname());
        dataAssetQueryOneVO.setDatabaseEname(one.getDatabaseEname());
        dataAssetQueryOneVO.setCatalogueId(one.getCatalogueId());
        List<DataAssetFieldInfo> list = dataAssetFieldService.getOneByDatabaseId(id);
        dataAssetQueryOneVO.setDataAssetFieldInfos(list);
        return dataAssetQueryOneVO;
    }
}
